package com.tchy.cloud.service.impl;

import com.tchy.cloud.entity.*;
import com.tchy.cloud.service.ABankService;
import com.tchy.cloud.service.CQuestionnaireService;
import org.apache.commons.lang.StringUtils;
import org.jeecgframework.core.common.service.CommonService;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/1/13.
 */
@Service
@Transactional
public class CQuestionnaireServiceImpl extends CommonServiceImpl implements CQuestionnaireService {
    @Autowired private CommonService commonService;
    @Override
    public void importing(String ids) throws Exception {
        String[] qtemId = ids.split(",");
        for (int mm = 0; mm < qtemId.length; mm++) {
            //量表模版对象id
            Integer qid = Integer.valueOf(qtemId[mm]);//模版量表id
            /**
             * 首先对量表进行拷贝
             */
//               获取量表模版的实体对象
            CQuestionnaireEntity quesEntity = commonService.getEntity(CQuestionnaireEntity.class, qid);

            //量表模版拷贝及保存
            CQuestionnaireEntity insert_quesEntity = new CQuestionnaireEntity();
            MyBeanUtils.copyBeanNotNull2Bean(quesEntity, insert_quesEntity);
            insert_quesEntity.setName("来源于_" + insert_quesEntity.getName() + "_量表");
            insert_quesEntity.setCreateTime(new Date());
            commonService.save(insert_quesEntity);
            // Integer new_qid = insert_quesEntity.getId();//量表拷贝后生成的新的量表id

            /**
             * 拷贝量表对应的量表纬度
             */
            List new_dimid_list = new ArrayList();//拷贝后新的量表对应的新的纬度id
            List<CQDimEntity> qDimEntity_list = commonService.findByProperty(CQDimEntity.class, "questEntity.id", qid);//模版量表对应的纬度表实体对象
            for (int t1 = 0; t1 < qDimEntity_list.size(); t1++) {
                CQDimEntity dimEntity = qDimEntity_list.get(t1);
                Integer old_dimid = dimEntity.getId();//旧的量表纬度id
                if (dimEntity.getDeleted() == 0) {////未被删除
                    CQDimEntity insert_dimEntity = new CQDimEntity();//新插入的纬度实体对象
                    MyBeanUtils.copyBeanNotNull2Bean(dimEntity, insert_dimEntity);//将原来的纬度实体拷贝到新的纬度实体中
                    insert_dimEntity.setName("来源于_" + insert_dimEntity.getName() + "_纬度");//更新名称
                    insert_dimEntity.setCreateTime(new Date());//更新创建时间
                    insert_dimEntity.setQuestEntity(insert_quesEntity);//更新对应的新的量表
                    commonService.save(insert_dimEntity);

                    Integer new_dimid = Integer.valueOf(String.valueOf(insert_dimEntity.getId()));
                    new_dimid_list.add(new_dimid);

                    /**
                     * 拷贝量表纬度对应的纬度结果
                     */
                    List<CScoreResultEntity> scoreResultEntity_list = commonService.findByProperty(CScoreResultEntity.class, "dimEntity.id", old_dimid);
                    for (int i = 0; i < scoreResultEntity_list.size(); i++) {
                        CScoreResultEntity temp_resutEntity = scoreResultEntity_list.get(i);
                        CScoreResultEntity insert_resutEntity = new CScoreResultEntity();
                        MyBeanUtils.copyBeanNotNull2Bean(temp_resutEntity, insert_resutEntity);//将原来的纬度结果实体拷贝到新的纬度结果实体中
                        insert_resutEntity.setDimEntity(insert_dimEntity);
                        commonService.save(insert_resutEntity);
                    }

                }
            }

            /**
             * 拷贝量表问题
             */

            List old_qqid_list = new ArrayList();//旧的问题id集合
            List new_qqid_list = new ArrayList();//新的问题id集合

            List new_qoid_list = new ArrayList();//新的（问题对应选项）id
            List<CQuesQuesEntity> quesquesEntity_list = commonService.findByProperty(CQuesQuesEntity.class, "questEntity.id", qid);
            for (int t2 = 0; t2 < quesquesEntity_list.size(); t2++) {
                CQuesQuesEntity qqEntity = quesquesEntity_list.get(t2);
                Integer old_qqid = qqEntity.getId();//原来的问题id
                old_qqid_list.add(old_qqid);
                if (qqEntity.getDeleted() == 0 && qqEntity.getEffective() == 1) {//未被删除并且有效
                    CQuesQuesEntity insert_qqEntity = new CQuesQuesEntity();
                    MyBeanUtils.copyBeanNotNull2Bean(qqEntity, insert_qqEntity);
                    insert_qqEntity.setQuestEntity(insert_quesEntity);
                    insert_qqEntity.setCreateTime(new Date());
                    commonService.save(insert_qqEntity);


                    Integer new_qqid = insert_qqEntity.getId();//新的问题id
                    new_qqid_list.add(new_qqid);


                    /**
                     * 拷贝问题的选项
                     */
                    List<CQQOptionEntity> qqOptionEntity_list = commonService.findByProperty(CQQOptionEntity.class, "questEntity.id", old_qqid);
                    for (int t3 = 0; t3 < qqOptionEntity_list.size(); t3++) {
                        {
                            CQQOptionEntity qqOptionEntity = qqOptionEntity_list.get(t3);
                            if (qqOptionEntity.getDeleted() == 0) {
                                CQQOptionEntity insert_qqOptionEntity = new CQQOptionEntity();
                                MyBeanUtils.copyBeanNotNull2Bean(qqOptionEntity, insert_qqOptionEntity);
                                insert_qqOptionEntity.setQuestEntity(insert_qqEntity);
                                insert_qqOptionEntity.setCreateTime(new Date());
                                commonService.save(insert_qqOptionEntity);

                                new_qoid_list.add(insert_qqOptionEntity.getId());
                            }
                        }
                    }
                }
            }

            /**
             *
             *量表纬度中关联的问题id进行替换
             */
            for (int n = 0; n < new_qqid_list.size(); n++) {//新的问题集合
                Integer temp_old_qqid = Integer.valueOf(String.valueOf(old_qqid_list.get(n)));//旧的问题id
                Integer temp_new_qqid = Integer.valueOf(String.valueOf(new_qqid_list.get(n)));//新的问题id

                for (int k = 0; k < new_dimid_list.size(); k++) {
                    CQDimEntity temp_dimEntity = commonService.getEntity(CQDimEntity.class, Integer.valueOf(String.valueOf(new_dimid_list.get(k))));//新的量表纬度对象

                    //直接替换字段：公式中的id
                    String formula_es = temp_dimEntity.getFormula();//公式
                    if (StringUtil.isNotEmpty(formula_es)) {
                        formula_es = formula_es.replace("Q" + temp_old_qqid + "Q", "Q" + temp_new_qqid + "Q");
                    }
                    //遍历替换字段：问题关联 中的id
                    String relQues_es = temp_dimEntity.getRelQues();//问题关联
                    if (StringUtil.isNotEmpty(relQues_es)) {
                        String[] relQues_es_spile = relQues_es.split(",");
                        for (int t = 0; t < relQues_es_spile.length; t++) {
                            if (String.valueOf(temp_old_qqid).equals(relQues_es_spile[t])) {
                                relQues_es_spile[t] = String.valueOf(temp_new_qqid);
                                relQues_es = StringUtils.join(relQues_es_spile, ",");

                                temp_dimEntity.setFormula(formula_es);
                                temp_dimEntity.setRelQues(relQues_es);
                                commonService.saveOrUpdate(temp_dimEntity);
                                break;
                            }
                        }

                    }
                }


                /**
                 *
                 * 选项中关联的问题id替换
                 */
                for (int p = 0; p < new_qoid_list.size(); p++) {
                    CQQOptionEntity temp_optionEntity = commonService.getEntity(CQQOptionEntity.class, Integer.valueOf(String.valueOf(new_qoid_list.get(p))));//新的问题选项对象
                    //遍历替换字段：不回答项 中关联的问题id
                    String as_noanswe = temp_optionEntity.getNoanswer();
                    if (StringUtil.isNotEmpty(as_noanswe)) {
                        String[] as_noanswe_spile = as_noanswe.split(",");
                        for (int t = 0; t < as_noanswe_spile.length; t++) {
                            if (String.valueOf(temp_old_qqid).equals(as_noanswe_spile[t])) {
                                as_noanswe_spile[t] = String.valueOf(temp_new_qqid);
                                as_noanswe = StringUtils.join(as_noanswe_spile, ",");

                                temp_optionEntity.setNoanswer(as_noanswe);
                                commonService.saveOrUpdate(temp_optionEntity);
                                break;
                            }

                        }
                    }
                }
            }
        }
    }
}
